package de.schlinke_marius.activities;

import java.util.ArrayList;
import java.util.List;

import android.app.Application;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager.OnPageChangeListener;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

import de.schlinke_marius.R;
import de.schlinke_marius.adapters.AppPagerAdapter;
import de.schlinke_marius.application.Constants;
import de.schlinke_marius.application.MSComputerApplication;
import de.schlinke_marius.data.AppContainer;
import de.schlinke_marius.database.MSCDatabase;
import de.schlinke_marius.dialogs.FaqDialog;
import de.schlinke_marius.fragments.ConsultingFragment;
import de.schlinke_marius.fragments.ExamplesFragment;
import de.schlinke_marius.fragments.HomeFragment;
import de.schlinke_marius.utils.AppUtils;
import de.schlinke_marius.views.ControllableViewPager;

public class MainTabActivity extends SherlockFragmentActivity implements
		OnPageChangeListener, ActionBar.TabListener, Constants {

	/*
	 * variables
	 */
	private static final String TAG = MainTabActivity.class.getSimpleName();
	private int visibleFragmentPosition = 0;
	private String emailAdress = "marius.schlinke@yahoo.de";
	private AppContainer globalData = null;
	private MSCDatabase database = null;
	
	
	/*
	 * UI elements
	 * 
	 */	
	private ActionBar actionBar;	
	private AppPagerAdapter appPagerAdapter = null;
	private ControllableViewPager vpFragments = null;
	
	/*
	 * fragments
	 * 
	 */
	private HomeFragment homeFragment = null;
	private ExamplesFragment examplesFragment = null;
	private ConsultingFragment consultingFragment = null;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
	
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_tab_activity);
		
		globalData = AppUtils.geGlobalDataOrExitActivity(this);
		if (globalData == null) {
			return;
		}

		initalizeRef();
		initializeViewPager();
		initialActionBar(savedInstanceState);

	}

	private void initalizeRef() {
		actionBar = getSupportActionBar();
	}
	
	
	private void initializeViewPager() {
		vpFragments = (ControllableViewPager) findViewById(R.id.vpFragments);
		vpFragments.setOnPageChangeListener(this);
		List<Fragment> fragments = new ArrayList<Fragment>();

		fragments.add(HomeFragment.instantiate(this, HomeFragment.class.getName()));
		fragments.add(ExamplesFragment.instantiate(this, ExamplesFragment.class.getName()));
		fragments.add(ConsultingFragment.instantiate(this, ConsultingFragment.class.getName()));
		appPagerAdapter = new AppPagerAdapter(getSupportFragmentManager(), fragments);
		vpFragments = (ControllableViewPager) findViewById(R.id.vpFragments);
		vpFragments.setAdapter(appPagerAdapter);
		vpFragments.setOnPageChangeListener(this);

		homeFragment = (HomeFragment) appPagerAdapter.getItem(0);
		examplesFragment = (ExamplesFragment) appPagerAdapter.getItem(1);
		consultingFragment = (ConsultingFragment) appPagerAdapter.getItem(2);
	}


	private void initialActionBar(Bundle savedInstanceState) {
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		actionBar.setDisplayShowTitleEnabled(false);
		actionBar.setDisplayShowHomeEnabled(false);

		
		actionBar.addTab(actionBar.newTab().setIcon(R.drawable.tabhome).setTabListener(this));
		actionBar.addTab(actionBar.newTab().setIcon(R.drawable.tabexamples).setTabListener(this));
		actionBar.addTab(actionBar.newTab().setIcon(R.drawable.tabconsulting).setTabListener(this));

		if (savedInstanceState != null) {
			visibleFragmentPosition = savedInstanceState.getInt("tab", visibleFragmentPosition);
		}
		actionBar.setSelectedNavigationItem(visibleFragmentPosition);
		
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		
			getSupportMenuInflater().inflate(R.menu.app_menu, menu);
		
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch (item.getItemId()) {

		case  R.id.menu_contact :  sendEmail();  return true;
		case R.id.menu_faq : showFaq(); return true;

		default:
			return false;
		}

	}

	private void sendEmail() {
		
		Intent mailIntent = new Intent();
		mailIntent.setAction(Intent.ACTION_SEND);
		mailIntent.setType("message/rfc822");
		mailIntent.putExtra(Intent.EXTRA_EMAIL,  new String[]{EMAIL_ADRESS});
		mailIntent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.email_subject));
		mailIntent.putExtra(Intent.EXTRA_TEXT, getString(R.string.email_text));
		startActivity(Intent.createChooser(mailIntent, "W�hlen Sie ein E-Mail Programm."));
	}
	private void showFaq() {
		
		FaqDialog faqDialog = FaqDialog.newInstance();
		faqDialog.show(getSupportFragmentManager(), "faqdialog");
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		outState.putInt("tab", visibleFragmentPosition);
		super.onSaveInstanceState(outState);
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageSelected(int position) {
		actionBar.setSelectedNavigationItem(position);
		visibleFragmentPosition = position;
		invalidateOptionsMenu();

	}

	@Override
	public void onTabSelected(Tab tab, FragmentTransaction ft) {
		vpFragments.setCurrentItem(tab.getPosition());
		
	}

	@Override
	public void onTabUnselected(Tab tab, FragmentTransaction ft) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onTabReselected(Tab tab, FragmentTransaction ft) {
		// TODO Auto-generated method stub
		
	}
	
	public void selectTab(int position) {
		actionBar.selectTab(actionBar.getTabAt(position));

		
	}
	@Override
	public void onStop() {
		super.onStop();
	}

}
